library(shiny)
library(tidyverse)

# Load precomputed data

sct_data <- readRDS(file = "data/rds/proj.rds")
sct_data_b <- readRDS(file = "data/rds/categories.rds")
choropleth_data <- readRDS(file = "data/rds/choropleth_data.rds")
bar_data  <- readRDS(file = "data/rds/bar_data.rds")
radar_data  <- readRDS(file = "data/rds/radar_data.rds")
tree_data <- readRDS(file = "data/rds/tree_data.rds")

#treemap+parallel coordinates data
treemap_data <- readRDS(file = "data/rds/treemap_data.rds")
pais <- read_delim("data/treemap/sumario-pais.csv", delim = ";", locale = locale(decimal_mark = "."))
regiao <- read_delim("data/treemap/sumario-regiao.csv", delim = ";", locale = locale(decimal_mark = "."))
uf <- read_delim("data/treemap/sumario-uf.csv", delim = ";", locale = locale(decimal_mark = "."))
meso <- read_delim("data/treemap/sumario-meso.csv", delim = ";", locale = locale(decimal_mark = "."))
micro <- read_delim("data/treemap/sumario-micro.csv", delim = ";", locale = locale(decimal_mark = "."))
municipio <- read_delim("data/treemap/sumario-municipio.csv", delim = ";", locale = locale(decimal_mark = "."))

heatmap_2012_data <- readRDS(file = "data/rds/heatmap_2012_data.rds")
heatmap_2013_data <- readRDS(file = "data/rds/heatmap_2013_data.rds")
heatmap_2014_data <- readRDS(file = "data/rds/heatmap_2014_data.rds")
heatmap_2015_data <- readRDS(file = "data/rds/heatmap_2015_data.rds")
heatmap_2016_data <- readRDS(file = "data/rds/heatmap_2016_data.rds")
heatmap_2017_data <- readRDS(file = "data/rds/heatmap_2017_data.rds")

choropleth_UF2012_data <- readRDS(file = "data/rds/percentage_UF2012.rds")
choropleth_UF2013_data <- readRDS(file = "data/rds/percentage_UF2013.rds")
choropleth_UF2014_data <- readRDS(file = "data/rds/percentage_UF2014.rds")
choropleth_UF2015_data <- readRDS(file = "data/rds/percentage_UF2015.rds")
choropleth_UF2016_data <- readRDS(file = "data/rds/percentage_UF2016.rds")
choropleth_UF2017_data <- readRDS(file = "data/rds/percentage_UF2017.rds")

choropleth_meso2012_data <- readRDS(file = "data/rds/percentage_meso2012.rds")
choropleth_meso2013_data <- readRDS(file = "data/rds/percentage_meso2013.rds")
choropleth_meso2014_data <- readRDS(file = "data/rds/percentage_meso2014.rds")
choropleth_meso2015_data <- readRDS(file = "data/rds/percentage_meso2015.rds")
choropleth_meso2016_data <- readRDS(file = "data/rds/percentage_meso2016.rds")
choropleth_meso2017_data <- readRDS(file = "data/rds/percentage_meso2017.rds")

server <- function(input, output, session) {

    observe({

        session$sendCustomMessage(type="scatterplot_values", message = list(sct_data, sct_data_b))

    })

    observe({

        session$sendCustomMessage(type="dendrogram_values",
                                  message = list(tree_data,
                                                 bar_data,
                                                 radar_data))

    })

    observe({

        session$sendCustomMessage(type="choropleth_values", message = list(
                                                                list(choropleth_UF2012_data,
                                                                     choropleth_UF2013_data,
                                                                     choropleth_UF2014_data,
                                                                     choropleth_UF2015_data,
                                                                     choropleth_UF2016_data,
                                                                     choropleth_UF2017_data),
                                                                list(heatmap_2012_data,
                                                                     heatmap_2013_data,
                                                                     heatmap_2014_data,
                                                                     heatmap_2015_data,
                                                                     heatmap_2016_data,
                                                                     heatmap_2017_data),
                                                                list(choropleth_meso2012_data,
                                                                     choropleth_meso2013_data,
                                                                     choropleth_meso2014_data,
                                                                     choropleth_meso2015_data,
                                                                     choropleth_meso2016_data,
                                                                     choropleth_meso2017_data)
                                                            )
                                  )

    })

    observe({
        session$sendCustomMessage(type="treemap_values", message = c(treemap_data,
                                                                      list(municipio,
                                                                           regiao)))

    })

# Comunication with treemap
    output$text <- renderText({
        req(input$scope)
        req(input$name)

        s <- input$scope
        v <- input$name

        switch(
            s,
            "pais"={
                filtered <- subset(municipio, pais  == v)
                foo <- regiao
            },
            "regiao"={
                filtered <- subset(municipio, regiao == v)
                foo <- subset(uf, regiao  == v)
            },
            "uf"={
                filtered <- subset(municipio, uf == v)
                foo <- subset(meso, uf  == v)
            },
            "mesorregiao"={
                filtered <- subset(municipio, mesorregiao == v)
                foo <- subset(micro, mesorregiao  == v)
            },
            "microrregiao"={
                filtered <- subset(municipio, microrregiao == v)
                foo <- subset(municipio, microrregiao  == v)
            },
            "municipio"={
                filtered <- subset(municipio, municipio == v)
                foo <- filtered
            }
        )

        session$sendCustomMessage(type="treemap2_values_change", message = list(filtered, foo))
    })


    observe({
        session$sendCustomMessage(type="parsets_values", message = 1))

    })
}
